<?php

namespace app\admin\model;
use think\Model;

/**
 * This is the model class for table "hxdc_xcx_kf_album".
 *
 * @property int $id
 * @property int $kf_id 顾问id
 * @property int $merchant_id 商户id
 * @property string $show_url 文件链接
 * @property string $fileId 腾讯云点播fileId
 * @property int $show_type 文件类型:1图片,2视频
 * @property string $title 图片、视频名称
 * @property int $sort 权重
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间	
 * @property int $status 状态[-1:删除;0:禁用;1启用]
 */
class XcxKfAlbum extends Model
{
    public static function paramsNew() :array {
        return array(
            'kf_id' => '',
            'show_url' => '链接',
            'fileId' => '云点播FileID',
            'show_type' => '类型',
            'title' => '标题',
            'sort' => '排序',
            'merchant_id' => 0,
            'status' => 1,
            'created_at' => time(),
            'updated_at' => time(),
        );
    }
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'hxdc_xcx_kf_album';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['kf_id'], 'required'],
            [['kf_id','merchant_id', 'show_type', 'sort', 'created_at', 'updated_at', 'status'], 'integer'],
            [['show_url'], 'string', 'max' => 255],
            [['fileId'], 'string', 'max' => 63],
            [['title'], 'string', 'max' => 20],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'merchant_id' => '商户 ID',
            'kf_id' => '顾问 ID',
            'show_url' => '链接',
            'fileId' => '云点播FileID',
            'show_type' => '类型',
            'title' => '标题',
            'sort' => '排序',
            'created_at' => '添加时间',
            'updated_at' => '更新时间',
            'status' => '状态',
        ];
    }
    /**
     * 关联顾问表
     *
     * @return \yii\db\ActiveQuery
     */
    public function getkf()
    {
        return $this->hasMany(XcxKf::class,['id' => 'kf_id']);
    }


    /**
     * @param bool $insert
     * @return bool
     * @throws \yii\base\Exception
     */
    public function beforeSave($insert)
    {
        if ($this->isNewRecord) {
            $this->created_at = time();
        }

        return parent::beforeSave($insert);
    }
    /**
     * @return array
     */
    public function behaviors()
    {
        $merchant_id = Yii::$app->services->merchant->getId();
        return [
            [
                'class' => TimestampBehavior::class,
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ],
            ],
            [
                'class' => BlameableBehavior::class,
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['merchant_id'],
                ],
                'value' => !empty($merchant_id) ? $merchant_id : 0,
            ]
        ];
    }
}
